package wnhz.com.business.freeshop.activity.fourthfree;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.suke.widget.SwitchButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.forget.AmentPasswordActivity;
import wnhz.com.business.groupshop.ui.AddressActivity;
import wnhz.com.business.bindphone.BoundPhoneActivity;
import wnhz.com.business.groupshop.ui.IdeaBackActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.rebateshop.activity.mine.AboutOurActivity;
import wnhz.com.business.uitls.CommomDialog;
import wnhz.com.business.uitls.DataCleanManager;
import wnhz.com.business.uitls.FastUtils;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.view.TranslucentActionBarW;

public class SettingActivity extends BaseActivity implements View.OnClickListener, ActionBarClickListener {


    @BindView(R.id.actionbars)
    TranslucentActionBarW actionbar;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.ll_pwd)
    LinearLayout llPwd;
    @BindView(R.id.switchView)
    SwitchButton switchView;
    @BindView(R.id.ll_feedback)
    LinearLayout llFeedback;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.ll_clear)
    LinearLayout llClear;
    @BindView(R.id.ll_rate)
    LinearLayout llRate;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.ll_aboutUs)
    LinearLayout llAboutUs;
    @BindView(R.id.tv_tuichu)
    TextView tvTuichu;
    private Activity activity;
    private final int TOST = 1;


    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        return intent;
    }

    private Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                //跳弹窗
                case TOST:
                    new CommomDialog(SettingActivity.this, R.style.dialog, "您确定清除缓存吗？", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            if (confirm == true) {

                                DataCleanManager.clearAllCache(getApplicationContext());
                                try {
                                    tvCache.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                dialog.dismiss();
                            } else {
                                dialog.dismiss();
                            }

                        }
                    }).show();
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_f);
        ButterKnife.bind(this);
        actionbar.setData("设置", R.drawable.ic_left_back, null, 0, "保存", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //改变状态栏颜色
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

        initView();
        initData();
    }

    private void initView() {
        llAddress.setOnClickListener(this);
        llPhone.setOnClickListener(this);
        llPwd.setOnClickListener(this);
        llClear.setOnClickListener(this);
        llFeedback.setOnClickListener(this);
        llRate.setOnClickListener(this);
        llAboutUs.setOnClickListener(this);
        tvTuichu.setOnClickListener(this);
        tvCode.setVisibility(View.GONE);
        //消息提醒开关
        switchView.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                if (isChecked == true) {

                    ToastUtils.showToast(SettingActivity.this, "已开启消息提醒");
                } else {
                    ToastUtils.showToast(SettingActivity.this, "关闭消息提醒");
                }
            }
        });
    }

    private void initData() {
        try {
            tvCache.setText("" + DataCleanManager.getTotalCacheSize(SettingActivity.this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //清理缓存
            case R.id.ll_clear:
                mhandler.sendEmptyMessage(TOST);
                break;

            case R.id.ll_address:
                if (!FastUtils.isFastDoubleClick()) {
                    startActivity(AddressActivity.createIntent(SettingActivity.this, "0"));
                }
                break;
            case R.id.ll_phone:
                if (!FastUtils.isFastDoubleClick()) {
                    startActivity(BoundPhoneActivity.createIntent(SettingActivity.this));
                }
                break;
            case R.id.ll_pwd:
                if (!FastUtils.isFastDoubleClick()) {
                    startActivity(AmentPasswordActivity.createIntent(SettingActivity.this));
                }
                break;
            case R.id.ll_feedback:
                if (!FastUtils.isFastDoubleClick()) {
                    startActivity(IdeaBackActivity.createIntent(SettingActivity.this));
                }
                break;
            case R.id.ll_rate: //去评分

                break;
            case R.id.ll_aboutUs://关于我们
                //要做判断，判断关于返利还是关于拼团或者免费
                if (!FastUtils.isFastDoubleClick()) {
                    startActivity(AboutOurActivity.createIntent(SettingActivity.this));
                }
                break;
            case R.id.tv_tuichu:
                MyApplication.getInstance().exit(activity);
                break;
            default:
                break;
        }
    }

    /**
     * ----弹窗确认是否清除缓存
     */
    private void cleanDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("是否清除缓存");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DataCleanManager.clearAllCache(getApplicationContext());
                setDataCleanManager();
            }
        });
        builder.show();
    }

    /**
     * ----清除缓存
     */
    private void setDataCleanManager() {
        try {
            tvCache.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {
    }

    @Override
    public void onEditTextClick() {

    }

}
